package com.kakaxi.mybilibili.module.home.category.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.CategoryAreaFirstRecyAdapter;
import com.kakaxi.mybilibili.adapter.entity.CategoryAreaFirstMultiItem;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.base.MyApplication;
import com.kakaxi.mybilibili.bean.CategoryAreaVideoItemBean;
import com.kakaxi.mybilibili.bean.CategoryHomeItemBean;
import com.kakaxi.mybilibili.bean.EventBusObject;
import com.kakaxi.mybilibili.module.home.category.presenter.CategoryAreaFirstPresenter;
import com.kakaxi.mybilibili.module.home.category.presenter.ICategoryAreaFirstPresenter;
import com.kakaxi.mybilibili.module.video.view.VideoActivity;
import com.kakaxi.mybilibili.utils.CastUncheckedUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 分类区域首个碎片类
 * Created by kakaxi on 16/11/28.
 */

public class CategoryAreaFirstFragment extends BaseFragment implements ICategoryAreaFirstView {

    @BindView(R.id.recv_common_fragment)
    RecyclerView recyclerView;

    @BindView(R.id.srl_common_fragment)
    SwipeRefreshLayout swipeRefreshLayout;

    /** 区名参数 **/
    private static final String PARAM_TYPENAME = "typename";
    /** 区id参数 **/
    private static final String PARAM_TID = "tid";

    /** 区名 **/
    private String mTypename;
    /** 区id **/
    private int mTid;

    private List<CategoryHomeItemBean> mChildList;

    private ICategoryAreaFirstPresenter mPresenter;

    private CategoryAreaFirstRecyAdapter mAdapter;

    public static CategoryAreaFirstFragment newInstance(String typename, int tid) {
        CategoryAreaFirstFragment categoryAreaFirstFragment = new CategoryAreaFirstFragment();
        categoryAreaFirstFragment.setTitle(typename);

        Bundle bundle = new Bundle();
        bundle.putString(PARAM_TYPENAME, typename);
        bundle.putInt(PARAM_TID, tid);
        categoryAreaFirstFragment.setArguments(bundle);
        return categoryAreaFirstFragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_common_recycler;
    }

    @Override
    protected void setupViews(ViewGroup viewGroup) {
        ButterKnife.bind(this, viewGroup);

        initRecyclerViewListener();
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refreshCategoryAreaFirstData(mTid);
            }
        });
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        mTypename = bundle.getString(PARAM_TYPENAME);
        mTid = bundle.getInt(PARAM_TID);

        Object object = ((MyApplication) getActivity().getApplication()).getShareData
                (CategoryAreaActivity.EXTRA_SHARE_CHILD_LIST);
        mChildList = CastUncheckedUtil.cast(object);

        mPresenter = new CategoryAreaFirstPresenter(this);
        mPresenter.initCategoryAreaFirstData(mTid);
    }

    @Override
    public List<CategoryHomeItemBean> getButtonBarList() {
        return mChildList;
    }

    @Override
    public void showCategoryAreaFirstData(List<CategoryAreaFirstMultiItem>
                                                  categoryAreaFirstMultiItemList) {
        if (mAdapter == null) {
            mAdapter = new CategoryAreaFirstRecyAdapter(categoryAreaFirstMultiItemList);
            recyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.setNewData(categoryAreaFirstMultiItemList);
        }
    }

    @Override
    public void showCategoryAreaFirstAllData(List<CategoryAreaFirstMultiItem>
                                                     categoryAreaFirstMultiItemList) {
        if (mAdapter != null) {
            mAdapter.addData(categoryAreaFirstMultiItemList);
        }
    }

    @Override
    public void showLoading() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showMessage(String message) {
        toastHandler(message);
    }

    private void initRecyclerViewListener() {
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int pos) {
                CategoryAreaFirstMultiItem item = mAdapter.getItem(pos);
                int itemType = item.getItemType();
                Object object = item.getData();
                switch (itemType) {
                    case CategoryAreaFirstMultiItem.TYPE_HEADER_BANNER: // 轮播

                        break;
                    case CategoryAreaFirstMultiItem.TYPE_HEADER_BUTTON_BAR: // 按钮组
                        break;
                    case CategoryAreaFirstMultiItem.TYPE_HEADER_RECOMMEND: // 热门推荐

                        break;
                    case CategoryAreaFirstMultiItem.TYPE_HEADER_NEW: // 最新视频
                        EventBusObject eventBusObject = new EventBusObject(EventBusObject
                                .EVENT_CATEGORY_AREA_ACT_TURN_PAGE, "");
                        EventBus.getDefault().post(eventBusObject);
                        break;
                    case CategoryAreaFirstMultiItem.TYPE_HEADER_ALL: // 全区动态
                        break;
                    case CategoryAreaFirstMultiItem.TYPE_CONTENT: // 正文
                        CategoryAreaVideoItemBean itemBean = (CategoryAreaVideoItemBean) object;
                        Bundle bundle = new Bundle();
                        bundle.putString(VideoActivity.EXTRA_VIDEO_AID, itemBean.getAid());
                        bundle.putString(VideoActivity.EXTRA_VIDEO_COVER_URL, itemBean.getPic());
                        goActivity(VideoActivity.class, bundle);
                        break;
                }
            }
        });
    }

}
